package xuanzi.h5.unittest;

import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.Element;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.RootPanel;

import sbaike.client.h5.client.ElUtils;

public abstract class UnitTestUI extends UnitTest{
	
	static int index;
	int errorIndex;
	int reviewIndex;
	int reportIndex;
	
	public String testId;

	public UnitTestUI(String name) {
		super(name); 
		testId = "test_"+index++;
		rootEl = rootEl.bind(RootPanel.getBodyElement()).createDiv().attr("style", "border:1px solid #eee;margin:8px;"); 
		String namex = testId;
		rootEl.createA("#").attr("name", namex).attr("href", "#"+namex).attr("style", "float:right;font-size:11px;color:#aaa;margin:3px;");
	}
	
	ElUtils rootEl; 
	
	@Override
	public void start() { 
		super.start();
		
	}
	
	public ElUtils getOutEl() {
		ElUtils out = rootEl.createDiv().attr("style", "padding:8px;background:#fff");
		String name = testId+"_review_"+reviewIndex++;
		out.createA("review").attr("name", name).attr("href","#" + name).attr("style", "float:right;font-size:11px;color:#aaa;").attr("title", "刷新后定位到这里");
		return out;
	}
	
	public Element outEl() {
		return getOutEl().toElement();
	}

	@Override
	public void pass(String message) { 
		super.pass(message);
		ElUtils itemEl = rootEl.createDiv().attr("style", "border-top:1px solid #eee;background:rgba(0,255,0,0.05);padding-left:8px");

		itemEl.createSpan(System.currentTimeMillis()-startTime+"ms  ").attr("style", "padding-right:18px;float:right;margin:3px;font-size:10px;color:#999");
		itemEl.createSpan(message).attr("style", passStyle());
	}


	@Override
	public void error(String message) { 
		super.error(message);
		errorIndex++;
		String name = testId+"_error_"+errorIndex;
		ElUtils itemEl = rootEl.createDiv().attr("style", "border-top:1px solid #eee;background:rgba(255,0,0,0.05);padding-left:8px");
		itemEl.createA(" #"+errorIndex).attr("name", name).attr("href", "#" +name).attr("style", "float:right;font-size:10px;color:#aaa;margin-right:3px").attr("title", "刷新后定位到该异常");
		itemEl.createSpan(System.currentTimeMillis()-startTime+"ms").attr("style", "float:right;margin:3px;font-size:11px;color:#999");
		itemEl.createSpan("❌ "+message).attr("style", errorStyle());
		
	}
	
	protected void log(String text, String css) { 
		Console.log(text, css);
		ElUtils itemEl = rootEl.createDiv().attr("style", "padding:3px");
		itemEl.createSpan(text.replace("%c", " ")).attr("style", css+";padding-left:5px");
	}
	
	@Override
	public void report() { 
		String name = testId+"_report_"+reportIndex++;
		rootEl.createA("#report").attr("name", name).attr("href", "#" +name).attr("style", "float:right;font-size:11px;color:#aaa;").attr("title", "刷新后定位到这里");
		super.report();
	}
	
	public static native Element getElementByName(String name)/*-{
		var list = $wnd.document.getElementsByName(name);
		if(list.length>0){
			return list[0];
		}else
			return null;
	}-*/;
	
	static Timer  timer = new Timer() {
		
		@Override
		public void run() {
			String url = Window.Location.getHref();
			if(url.contains("#")) {
				int s = url.lastIndexOf("#");
				int e = url.indexOf("&");
				if(e==-1)
					e = url.length();
				String name = url.substring(s+1,e); 
				Element byname = getElementByName(name);
				Console.log("go to "+name);
				if(byname!=null) {
					byname.scrollIntoView();
					byname.getParentElement().getStyle().setProperty("borderTop", "1px dotted red");
					Document.get().setScrollTop(Document.get().getScrollTop()+Window.getClientHeight()/2);
				}
			}
		}
	};
	
	@Override
	public void finish() { 
		super.finish();
		if(error>0) {
			rootEl.style().setProperty("border", "1px dotted red");
			rootEl.style().setProperty("border-bottom", "5px solid red");
		}else {
			rootEl.style().setProperty("border", "1px dotted green");
			rootEl.style().setProperty("border-bottom", "5px solid green");
		}
		//自动定位到指定的锚点
		if(timer.isRunning())
			timer.cancel();
		timer.schedule(300);
	}
	
}
